package cn.itsource.controller;

import cn.itsource.annotation.PreAuthorize;
import cn.itsource.domain.Permission;
import cn.itsource.domain.Permission;
import cn.itsource.query.PermissionQuery;
import cn.itsource.service.IPermissionService;
import cn.itsource.util.R;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Api(tags = "权限接口")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;
    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(sn="permission:del", name = "权限删除",descs = "权限删除")
    public R del(@PathVariable("id") Long id){
        try {
            permissionService.del(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    @PreAuthorize(sn="permission:delList", name = "权限批量删除",descs = "权限批量删除")
    public R delList(@RequestBody List<Long> ids){
        try {
            permissionService.delList(ids);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
    /**
     * 分页查询
     * @param query
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    @PreAuthorize(sn="permission:pageQuery", name = "权限分页查询",descs = "权限分页查询")
    public PageInfo<Permission> pageQuery(@RequestBody PermissionQuery query){
        return permissionService.pageQuery(query);
    }
    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<Permission> getAll(){
        return permissionService.getAll();
    }
}
